<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/28
 * Time: 14:20
 */
namespace Controller;

//登录控制器
use Shop\Controller;

class LoginController extends Controller {
    //自动登录
    public function __construct()
    {
        parent::__construct();

        //判断是否有cookie
        if(!empty($_COOKIE['admin_name'])){
            //调用自定义模型实现自动登录
            $obj=new \Model\LoginModel();
            $arr=$obj->AgetUser($_COOKIE['admin_name']);

            if($arr){
                //保存session
                $_SESSION[SHOP]['user']=$arr;
                //修改ip和数据
                $obj->AsetUser($arr['name']);
                //跳转
                $this->loca("Index",'index');
            }
        }
    }

    //登录
    public function login(){

        if($_POST){
            //提交表单
            if(!$_POST['name']||!$_POST['pass']||!$_POST['yzm']){
                $this->success('请输入完整信息');
            }
            //判断验证码是否正确
            if($_SESSION[SHOP]['yzm']!=$_POST['yzm']){
                $this->success('验证码错误');
            }
            //调用模型,实现登录
            $obj=new \Model\LoginModel();
            $is=$obj->Alogin($_POST['name'],$_POST['pass']);
            if(!$is){
                //判断是否记住登录状态
                if($_POST['online']=='1'){
                    //保存信息到cookie
                    setcookie('admin_name',$_SESSION[SHOP]['user']['name'],time()+3*24*3600,'/');
                }
                $this->success('登录成功','Index','index');
            }else{
                $this->success($is);
            }
        }
        else{
            //显示页面
            $this->view->display();
        }

    }

    //验证码
    public function idYzm(){
        $this->yzm(4);
    }
}